﻿#pragma once
#include "wx/wx.h"
#include "wxplot.hpp"
#include <map>
#include <memory>
#include <boost/circular_buffer.hpp>

wxDECLARE_EVENT(WAVE_EVENT_CLEAR_DATA, wxCommandEvent);

class WaveEventNewPlotData;
wxDECLARE_EVENT(WAVE_EVENT_NEW_PLOT_DATA, WaveEventNewPlotData);

class WaveformPanel : public wxPlot {
public:
    struct WaveData {
        int tag;
        double x, y;
    };

    WaveformPanel();
    WaveformPanel(wxWindow* parent);
    void AsyncClearData();
    void AsyncPushData(int tag, double x, double y);

protected:
    void OnSize(wxSizeEvent& event);
    void OnClearData(wxCommandEvent& event);
    void OnNewPlotData(WaveEventNewPlotData& event);

private:
    wxDECLARE_DYNAMIC_CLASS_NO_COPY(WaveformPanel);
    std::map<int, std::unique_ptr<boost::circular_buffer<WaveData>>> waveBuffers;
};


class WaveEventNewPlotData : public wxEvent {
public:
    WaveEventNewPlotData(int tag, double x, double y)
        : wxEvent(0, WAVE_EVENT_NEW_PLOT_DATA)
        , wtag(tag)
        , wx(x)
        , wy(y)
    {}

    WaveformPanel::WaveData GetWaveData() const {
        WaveformPanel::WaveData data{};
        data.x = wx;
        data.y = wy;
        data.tag = wtag;
        return data;
    }

    wxEvent* Clone() const override {
        return new WaveEventNewPlotData(wtag, wx, wy);
    }

private:
    int wtag;
    double wx, wy;
};